package com.example.celldian.db;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdornment extends SQLiteOpenHelper {

	public DBAdornment(Context activity) {
		super(activity, "Celldian.db", null, 1);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		String query = "CREATE TABLE adornments ( itemname TEXT PRIMARY KEY, resistence TEXT, strength TEXT, " +
					   "intelect TEXT, agility TEXT, stamina TEXT, part TEXT, type TEXT, " + 
					   "class TEXT, bonusname TEXT)";
		
		db.execSQL(query);	
		
		Log.i("DATABASE DBAdornment:","TABLE adornments SUCCESSFULY CREATED!");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		String query = "DROP TABLE IF EXISTS adornments";
		
		db.execSQL(query);
		onCreate(db);				
		
		Log.i("DATABASE DBAdornment:","UPDATE SUCCESSFUL!");
	}
	
	public void insertAdornment(HashMap<String ,String> adornmentValues) {
			
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("itemname", adornmentValues.get("itemname"));
		values.put("resistence", adornmentValues.get("resistence"));
		values.put("strength", adornmentValues.get("strength"));
		values.put("intelect", adornmentValues.get("intelect"));
		values.put("agility", adornmentValues.get("agility"));
		values.put("stamina", adornmentValues.get("stamina"));
		values.put("part", adornmentValues.get("part"));
		values.put("type", adornmentValues.get("type"));
		values.put("class", adornmentValues.get("class"));
		values.put("bonusname", adornmentValues.get("bonusname"));
		
		db.insert("adornments", null, values);
		db.close();
		
		Log.i("DATABASE DBAdornment:","ADORNMENT INSERT WAS SUCCESSFUL!");
	}
	
	public int updateAdornment(HashMap<String ,String> adornmentValues) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("itemname", adornmentValues.get("itemname"));
		values.put("resistence", adornmentValues.get("resistence"));
		values.put("strength", adornmentValues.get("strength"));
		values.put("intelect", adornmentValues.get("intelect"));
		values.put("agility", adornmentValues.get("agility"));
		values.put("stamina", adornmentValues.get("stamina"));
		values.put("part", adornmentValues.get("part"));
		values.put("type", adornmentValues.get("type"));
		values.put("class", adornmentValues.get("class"));
		values.put("bonusname", adornmentValues.get("bonusname"));
		
		Log.i("DATABASE DBAdornment:","ADORNMENT UPDATE WAS SUCCESSFUL!");
		
		return db.update("adornments", values, "itemname" + "?=", new String[] {adornmentValues.get("itemname")});
	}
	
	public void deleteAdornment(String itemname) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		String query = "DELETE FROM adornments WHERE itemname='" + itemname + "'";
		
		db.execSQL(query);
		
		Log.i("DATABASE DBAdornment:","ARMOR DELETE WAS SUCCESSFUL!");
	}
	
	public ArrayList<HashMap<String, String>> getAdornments() {
		
		ArrayList<HashMap<String, String>> adornmentList = new ArrayList<HashMap<String, String>>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM adornments";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				HashMap<String, String> adornments = new HashMap<String, String>();
				
				adornments.put("itemname", cursor.getColumnName(0));
				adornments.put("resistence", cursor.getColumnName(1));
				adornments.put("strength", cursor.getColumnName(2));
				adornments.put("intelect", cursor.getColumnName(3));
				adornments.put("agility", cursor.getColumnName(4));
				adornments.put("stamina", cursor.getColumnName(5));
				adornments.put("part", cursor.getColumnName(6));
				adornments.put("type", cursor.getColumnName(7));
				adornments.put("class", cursor.getColumnName(8));
				adornments.put("bonusname", cursor.getColumnName(9));
				
				adornmentList.add(adornments);
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBAdornment:","ADORNMENTS RETURN WAS SUCCESSFUL!");
		
		return adornmentList;
	}
	
	public HashMap<String, String> getAdornment(String itemname){
		
		HashMap<String, String> adornments = new HashMap<String, String>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM adornments WHERE itemname='" + itemname + "'";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				adornments.put("itemname", cursor.getColumnName(0));
				adornments.put("resistence", cursor.getColumnName(1));
				adornments.put("strength", cursor.getColumnName(2));
				adornments.put("intelect", cursor.getColumnName(3));
				adornments.put("agility", cursor.getColumnName(4));
				adornments.put("stamina", cursor.getColumnName(5));
				adornments.put("part", cursor.getColumnName(6));
				adornments.put("type", cursor.getColumnName(7));
				adornments.put("class", cursor.getColumnName(8));
				adornments.put("bonusname", cursor.getColumnName(9));
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBAdornment:","ADORNMENT RETURN WAS SUCCESSFUL!");
		
		return adornments;
	}
}
